﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Threading;
using Yugi.Client.WebSerive;

namespace WpfApplication1
{
	/// <summary>
	/// Interaction logic for ListUser.xaml
	/// </summary>
	public partial class ListUser : UserControl
	{
		public ListUser()
		{
			this.InitializeComponent();
            this.Loaded += new RoutedEventHandler(ListUser_Loaded);
		}

        void LoadData()
        {
           
        }
        public void AddUser(PlayerInfo obj)
        {
            User user = new User();
            user.ID = (int)obj.ID;
            user.DataContext = obj;
            user.username.Text = obj.NickName;
            user.level.Text = obj.Level.ToString(); ;
            user.state.Text = obj.IsOnline == 1 ? "online" : "offline";
            listView.Items.Add(user);
            user.OnClick += new Action<object>(User_OnClick);
            user.ContexMenu += new Action<object, object>(ContexMenu_Click);
        }
        void User_OnClick(object obj)
        {
            User tb = obj as User;
            if (tb == null) return;
            if (OnChoice != null)
                OnChoice(tb.DataContext);
        }
        void ListUser_Loaded(object sender, RoutedEventArgs e)
        {
            LoadData();
        }
        private void ContexMenu_Click(object obj, object obj2)
        {
            if (obj == null||obj2==null) return;
            if (ContexMenu != null)
                ContexMenu(obj, obj2);
        }
        public event Action<object> OnChoice;
        public event Action<object, object> ContexMenu;

        internal void RemoveUser(int playerid)
        {
            foreach(User i in listView.Items)
                if (i.ID == playerid)
                {
                    listView.Items.Remove(i);
                    return;
                }
        }


        public User GetUser(int UserID)
        {
            foreach (User i in listView.Items)
            {
                if (i.ID == UserID)
                    return i;
            }
            return null;
        }
    }
}